package cn.gailvlun.gll.presentation.chat;

import android.arch.lifecycle.ViewModelProviders;
import android.content.Context;
import android.content.Intent;
import android.databinding.DataBindingUtil;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.widget.LinearLayoutManager;

import com.blankj.utilcode.util.ActivityUtils;
import com.github.markzhai.recyclerview.BaseViewAdapter;

import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

import cn.gailvlun.gll.R;
import cn.gailvlun.gll.chatlib.event.FriendInviteEvent;
import cn.gailvlun.gll.net.base.friend.FriendNotification;
import cn.gailvlun.gll.presentation.base.BaseActivity;
import cn.gailvlun.gll.databinding.ActivityFriendNotificationBinding;
import cn.gailvlun.gll.net.HttpMethods;
import cn.gailvlun.gll.net.HttpSubscriber;
import cn.gailvlun.gll.net.base.friend.EnsureAddFriendReq;
import cn.gailvlun.gll.presentation.chat.adapter.FriendNotificationAdapter;
import cn.gailvlun.gll.presentation.chat.vm.FriendNotificationViewModel;
import cn.gailvlun.gll.util.DialogUtil;
import cn.gailvlun.gll.util.RxUtil;
import cn.gailvlun.gll.widget.NavigationBarView;
import io.reactivex.functions.Consumer;
import io.realm.RealmResults;



public class FriendNotificationActivity extends BaseActivity {

    public static void openActivity(Context context) {
        Intent intent = new Intent(context, FriendNotificationActivity.class);
        context.startActivity(intent);
    }

    public static void openFromNotification(Context context) {
        Intent intent = new Intent(context, FriendNotificationActivity.class);
        intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        context.startActivity(intent);
    }

    private ActivityFriendNotificationBinding mBinding;

    private FriendNotificationAdapter mAdapter;
    private LinearLayoutManager mLayoutManager;
    private FriendNotificationViewModel mViewModel;

    @Override
    protected void initComponent() {
        mBinding = DataBindingUtil.setContentView(this, R.layout.activity_friend_notification);

        mAdapter = new FriendNotificationAdapter(mContext);
        mAdapter.setPresenter(new Presenter());
        mBinding.recyclerView.setAdapter(mAdapter);

        mLayoutManager = new LinearLayoutManager(mContext);
        mBinding.recyclerView.setLayoutManager(mLayoutManager);

        mViewModel = ViewModelProviders.of(this).get(FriendNotificationViewModel.class);
    }

    @Override
    protected void createHandler() {
        mBinding.nbv.setOnBackListener(new NavigationBarView.OnBackListener() {
            @Override
            public void onBack(NavigationBarView nb) {
                onBackPressed();
            }
        });
    }

    @Override
    protected void loadData(@Nullable Bundle savedInstanceState) {
        mViewModel.getFriendNotificationFlowable()
                .subscribe(new Consumer<RealmResults<FriendNotification>>() {
                    @Override
                    public void accept(RealmResults<FriendNotification> friendNotifications) throws Exception {
                        mAdapter.set(friendNotifications);
                    }
                });
    }

    public class Presenter implements BaseViewAdapter.Presenter {
        
        public void onAgree(final FriendNotification item) {

            HttpMethods.getFriendService()
                    .ensureAddFriend(new EnsureAddFriendReq(item.getId(), 1))
                    .compose(RxUtil.<FriendNotification>applyScheduler())
                    .subscribe(new HttpSubscriber<FriendNotification>() {
                        @Override
                        protected void onSuccess(FriendNotification response) {
                            
                            mViewModel.saveFriendNotification(response);
                        }

                        @Override
                        protected void onFailure(String errMsg, @Nullable FriendNotification response, int code) {
                            if (code == 406) {
                                
                                
                                mViewModel.alreadyFriend(item);
                            }
                            DialogUtil.showError(mContext, errMsg);
                        }
                    });
        }
    }

    public static class Util {
        public static boolean getInviteEnabled(FriendNotification item) {
            return item.getStatus() == 0;
        }

        public static String getInviteText(FriendNotification item) {
            if (getInviteEnabled(item)) {
                return "接受";
            } else {
                return "已添加";
            }
        }
    }
}
